﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CollegeWebsite.Models;

namespace CollegeWebsite.Controllers
{
    public class AdmissionController : Controller
    {
        private readonly CollegeDataContext _dataContext = new CollegeDataContext();
       
        //
        // GET: /Admission/
       
        public ActionResult AdmissionForm()
        {

            var listFaculty = _dataContext.Faculties.Select(f=> f).Where(f => (f.Status == true)).ToList();
            var listDepartment = _dataContext.Departments.Select(d => d).Where(d => (d.Faculty == listFaculty.First())).ToList();
            var listCourse = _dataContext.Courses.Select(c => c).Where(c => (c.Department == listDepartment.First())).ToList();

            ViewData["listDepartment"] = listDepartment; 
            ViewData["selectedDepartment"] = listDepartment.First().DepartmentID;
            
            ViewData["listFaculty"] = listFaculty;
            ViewData["selectedFaculty"] = listFaculty.First().FacultyID;

            ViewData["listCourse"] = listCourse;
            ViewData["selectedCourse"] = listCourse.First().CourseID;

            return View();
        }
        [HttpPost]
        public ActionResult AdmissionForm(FormCollection formCollection)
        {

            var falcultyId = formCollection.Get("Faculty");
            var departmentId = formCollection.Get("Department");
            var courseId = formCollection.Get("Course"); 
            
            ViewData["listFaculty"] = _dataContext.Faculties.Select(f => f).Where(f => (f.Status == true)).OrderBy(f => f.FacultyName).ToList();
            ViewData["selectedFaculty"] = falcultyId;



            
              

       if(!String.IsNullOrEmpty(falcultyId))
        {
            int falId  = Convert.ToInt32(falcultyId);

            var listDepartment = _dataContext.Departments.Select(d => d).Where(d => (d.Status && d.FacultyID == falId)).OrderBy(
                    d => d.DepartmentName).ToList();
            ViewData["listDepartment"] = listDepartment;
            ViewData["selectedDepartment"] = departmentId;

        }


        
        if (!String.IsNullOrEmpty(departmentId))
        {
            int depId   = Convert.ToInt32(departmentId);

            //
            var listCourse = _dataContext.Courses.Select(c => c).Where(c => (c.Status==true && c.DepartmentID == depId)).ToList();
            ViewData["listCourse"] = listCourse;
            ViewData["selectedCourse"] = courseId;
        }
        else
        {
            ViewData["listCourse"] = new List<Course>();
        }

            return View("AdmissionForm");
        }


    

    }
}
